<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\HouseAudit;
use App\Models\IntermediaryUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class HouseAuditController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HouseAudit(['IntermediaryUser']), function (Grid $grid) {
            $grid->column('id')->sortable();
//            $grid->column('intermediary_id');
            $grid->column('intermediary_user.real_name');
            $grid->column('intermediary_user.mobile');
            $grid->column('house_name');
            $grid->column('house_area');
            $grid->column('house_address');
            $grid->column('house_desc');
            $grid->column('house_status');

            $grid->house_status->using([0 => '待审核', 1 => '审核不通过',2=>'审核通过',3=>'未租/售',4=>'已租/售' ])->badge([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default

                1 => 'danger',
                2 => 'primary',
                3 => 'success',
                4 => 'success',
                5 => 'success'
            ]);

            $grid->column('created_at');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // append一个操作
                $id = $actions->getKey();
                $actions->append("<a href='pass?id=$id'>通过</a>");

                // prepend一个操作
                $actions->prepend("<a href='reject?id=$id'>驳回</a>");
            });
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('Mobile','手机号');
                $filter->like('Real Name','昵称');
                $filter->like('house_name','房源');

            });
        });
    }

    public function pass(Request $request)
    {
        $id = $request->get('id');
        HouseAudit::pass($id);
        return redirect('admin/HouseAuditController');
    }
    public function reject(Request $request)
    {
        $id = $request->get('id');
        HouseAudit::reject($id);
        return redirect('admin/HouseAuditController');

    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HouseAudit(), function (Show $show) {
            $show->field('id');
            $show->field('intermediary_id');
            $show->field('house_name');
            $show->field('house_area');
            $show->field('house_address');
            $show->field('house_owner');
            $show->field('house_desc');
            $show->field('house_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HouseAudit(), function (Form $form) {
            $form->display('id');
            $form->text('intermediary_id');
            $form->text('house_name');
            $form->text('house_area');
            $form->text('house_address');
            $form->text('house_owner');
            $form->text('house_desc');
            $form->text('house_status');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}

